/* 
 * Copyright 2012 Andrew Brock
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.catchwa.myvideobrowser.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
import org.catchwa.myvideobrowser.Main;

/**
 *
 * @author Andrew Brock
 */
public class VideoExtensionsDialog extends JDialog {

  private JButton okButton, cancelButton, addButton, removeButton;
  private JList<String> list;

  public VideoExtensionsDialog() {
    this.setLayout(new BorderLayout());
    this.setTitle("Configure Video File Extensions");

    JPanel addRemoveButtonPanel = new JPanel(new GridLayout(1, 2));
    addButton = new JButton("Add...");
    addButton.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        String newExtension = JOptionPane.showInputDialog(null, "Add a new video file extension in the text box below (starting with a \".\"", "Add New Video File Extension", JOptionPane.QUESTION_MESSAGE);
        if(newExtension != null) {
          DefaultListModel dlm = (DefaultListModel) list.getModel();
          if(dlm.contains(newExtension)) {
            JOptionPane.showMessageDialog(null, "File extension \"" + newExtension+"\" already exists.", "Extension Already Exists", JOptionPane.ERROR_MESSAGE);
          }
          else if(! newExtension.startsWith(".")) {
            JOptionPane.showMessageDialog(null, "File extensions must start with a \".\"", "Invalid Extension", JOptionPane.ERROR_MESSAGE);
          }
          else if(newExtension.length() < 2) {
            JOptionPane.showMessageDialog(null, "Invalid file extension", "Invalid Extension", JOptionPane.ERROR_MESSAGE);
          } else {
            dlm.addElement(newExtension);
            String[] array = new String[dlm.size()];
            dlm.copyInto(array);
            Arrays.sort(array);
            dlm.removeAllElements();
            for(String s : array) {
              dlm.addElement(s);
            }
          }
        }
      }
    });
    
    removeButton = new JButton("Remove");
    removeButton.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        DefaultListModel dlm = (DefaultListModel) list.getModel();
        if (list.getSelectedIndices().length > 0) {
          int[] selectedIndices = list.getSelectedIndices();
          for (int i = selectedIndices.length - 1; i >= 0; i--) {
            dlm.removeElementAt(selectedIndices[i]);
          }
        }
      }
    });
    
    addRemoveButtonPanel.add(addButton);
    addRemoveButtonPanel.add(removeButton);
    this.add(addRemoveButtonPanel, BorderLayout.NORTH);
    
    DefaultListModel<String> listModel = new DefaultListModel<String>();
    for(String s : Main.getProperties().getStringArray("video.extensions")) {
      listModel.addElement(s);
    }
    list = new JList<String>(listModel);
    list.setAutoscrolls(true);
    JScrollPane listScrollPane = new JScrollPane(list);
    this.add(listScrollPane, BorderLayout.CENTER);
    
    JPanel okCancelButtonPanel = new JPanel(new GridLayout(1, 2));
    okButton = new JButton("OK");
    okButton.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        DefaultListModel dlm = (DefaultListModel) list.getModel();
        String[] values = new String[dlm.size()];
        dlm.copyInto(values);
        Main.getProperties().setProperty("video.extensions", Arrays.asList(values));
        dispose();
      }
    });
    
    cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        dispose();
      }
    });
    
    okCancelButtonPanel.add(okButton);
    okCancelButtonPanel.add(cancelButton);
    this.add(okCancelButtonPanel, BorderLayout.SOUTH);

    this.setSize(200, 400);
    this.setModal(true);
    this.setResizable(false);
    this.setVisible(true);
  }
}
